﻿using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace SBPweb.Forge.Controls.Silverlight.Utilities
{

    public class ImageSourceConverter : IValueConverter
    {

        //public static readonly DependencyProperty ImageSourceBindingPathFormatProperty = DependencyProperty.Register("ImageSourceBindingPathFormat", typeof(string), typeof(FrameworkElement), null);

        //public FrameworkElement ParentElement
        //{
        //    get;
        //    set;
        //}

        public string ImageSourceBindingPathFormat
        {
            get;
            set;
        }

        //public string ImageSourceBindingPathFormat
        //{
        //    get
        //    {
        //        if (ParentElement == null)
        //        {
        //            return null;
        //        }
        //        return ParentElement.GetValue(ImageSourceBindingPathFormatProperty) as string;
        //    }
        //    set
        //    {
        //        if (ParentElement == null)
        //        {
        //            return;
        //        }
        //        ParentElement.SetValue(ImageSourceBindingPathFormatProperty, value);
        //    }
        //}

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (targetType == typeof(ImageSource))
            {
                if(parameter == null && ImageSourceBindingPathFormat != null)
                {
                    parameter = ImageSourceBindingPathFormat;
                }
                string paramAsString = parameter.ToString();
                return new BitmapImage(new Uri(string.Format(paramAsString, value.ToString()), UriKind.Relative));
            }
            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }

    }

}